import http from '../core/http.service'
import {Common} from './common.state'
import unicore from '../core/core.service'
import unistore from './index'

/**
 * 公共action参数
 */
interface ActionArgs {
    state:{
		common:Common
	};
	commit:Function;
}

export default {
	setToken({ state, commit }: ActionArgs, data: string) {
		state.common.token = data;
	},
	/* 兴业微信公众号页面 */
	async submitwxidCard({state,commit}:ActionArgs,data:any){ // 提交身份信息
		const res = await http.post("/loan/xfjrPa/page/submitWxIdCardPub/v1",data);
		console.log(res)
		return res;
	},
	async getWxIdCardPub({state,commit}:ActionArgs){ // 我的身份查询信息
		const wxOpenId = unicore.dom.getQueryString("wxOpenId") || unicore.util.localStorage.get("wxOpenId")
		const res = await http.post("/loan/xfjrPa/page/getWxIdCardPub/v2",{wxOpenId});
		console.log(res)
		return res;
	},
	async getSmsCodePub({state,commit}:ActionArgs,data:any){ // 验证码
		const res = await http.post("/loan/sms/api/getSmsCodePub/v1",data);
		console.log(res)
		return res;
	},
	async getWxWaitRepayListPub({state,commit}:ActionArgs,data:any){ // 待还款列表查询
		const wxOpenId = unicore.dom.getQueryString("wxOpenId") || unicore.util.localStorage.get("wxOpenId")
		const res = await http.post("/loan/xfjrPlan/api/getWxWaitRepayListPub/v2",{wxOpenId});
		console.log(res)
		return res;
	},
	async getIdCard({state,commit}:ActionArgs,flag:any){
		const wxOpenId = unicore.dom.getQueryString("wxOpenId") || unicore.util.localStorage.get("wxOpenId")
		let url=location.href;
		if(!unicore.dom.getQueryString("wxOpenId")){
			url+="?wxOpenId=" + wxOpenId
		}
		console.log(url,'<-getidcard url')
		let params:any= {wxOpenId,
			url
		}
		if (flag) {
			params.flag = '1'
		}
		console.log(params,'shangchuan参数11')
		const res:any = await http.post("/loan/wxdetectauth/page/getWxIdCardPub/v2",params);
		if(res.data && res.data.url){
			unicore.util.localStorage.set("wxOpenId",wxOpenId)
			state.common.realUrl = res.data.url;
			if (flag) {
				console.log(params,'shangchuan参数22')
				return res
			}
			uni.navigateTo({
				url:'/pages/realnotice/realnotice'
			})
		} else if(res.data && res.data.idCardCodeHidden){
			state.common.idCardCodeHid = res.data.idCardCodeHidden
			state.common.idCardNameHid = res.data.idCardNameHidden
		}
		return res;
	},
	async getWxRepayPlanPub({state,commit}:ActionArgs,data:any){ // 单个还款计划查询
		const res = await http.post("/loan/xfjrPlan/api/getWxRepayPlanPub/v1",data);
		console.log(res)
		// state.common.singlePay = res
		return res;
	},
	async repayLoanQryPub({state,commit}:ActionArgs,data:any){ // 欠款查询
		const res = await http.post("/loan/xfjrTrans/api/repayLoanQryPub/v1",data);
		console.log(res)
		return res;
	},
	async wxUnifiedorderPub({state,commit}:ActionArgs,data:any){ // 微信还款统一下单
		const res = await http.post("/loan/wxpay/api/wxUnifiedorderPub/v1",data);
		console.log(res)
		return res;
	},
	async wxUnifiedordertestPub({state,commit}:ActionArgs,data:any){ // 微信还款统一下单
		const res = await http.post("/loan/wxpay/api/wxUnifiedordertestPub/v1",data);
		console.log(res)
		return res;
	},
	async getOrderDetailPub({state,commit}:ActionArgs,data:any){ // 微信支付查询状态
		const res = await http.post("/loan/wxpay/api/getOrderDetailPub/v1",data);
		console.log(res)
		return res;
	},
	async getWxPlanListPub({state,commit}:ActionArgs,data:any){ // 我的贷款银行卡签约列表查询
		const wxOpenId = unicore.dom.getQueryString("wxOpenId") || unicore.util.localStorage.get("wxOpenId")
		console.log(wxOpenId, 'wxOpenId')
		const res = await http.post("/loan/xfjrPlan/api/getWxPlanListPub/v2",{wxOpenId});
		console.log(res)
		return res;
	},
	async signWxBkCardPub({state,commit}:ActionArgs,data:any){ // 银行卡签约获取验证码
		const res = await http.post("/loan/wxSign/api/signWxBkCardPub/v1",data);
		console.log(res)
		return res;
	},
	async confirmWxBkCardPub({state,commit}:ActionArgs,data:any){ // 提交银行卡签约确认信息
		const res = await http.post("/loan/wxSign/api/confirmWxBkCardPub/v1",data);
		console.log(res)
		return res;
	},
	/* 兴业cliApp页面 */
	async checkAcountPub({state,commit}:ActionArgs,data:any){ // 验证手机号是否存在
		const res = await http.post("/loan/cust/api/checkAcountPub/v1",data);
		console.log(res)
		return res;
	},
	async getComCodePub({state,commit}:ActionArgs,data:any){ // 验证码
		const res = await http.post("/loan/cust/api/getComCodePub/v1",data);
		console.log(res)
		return res;
	},
	async getRestLoginPwdCodePub({state,commit}:ActionArgs,data:any){ // 验证码
		const res = await http.post("/loan/cust/api/getRestLoginPwdCodePub/v1",data);
		console.log(res)
		return res;
	},
	async restLogin({state,commit}:ActionArgs,data:any){ // 找回密码
		const res = await http.post("/loan/cust/api/restLoginWxPwdEncPub/v1",data);
		console.log(res)
		return res;
	},
	async registerThirdEncPub({state,commit}:ActionArgs,data:any){ // 注册
		const res = await http.post("/loan/cust/api/registerThirdEncPub/v1",data);
		console.log(res)
		return res;
	},
	async loginThirdEncPub({state,commit}:ActionArgs,data:any){ // 登录
		const res = await http.post("/loan/cust/api/loginThirdEncPub/v1",data);
		console.log(res)
		return res;
	},
	async realName({state,commit}:ActionArgs,bizToken:string){
		const wxOpenId = unicore.dom.getQueryString("wxOpenId") || unicore.util.localStorage.get("wxOpenId")
		try{
			var res = await http.post("/loan/wxdetectauth/api/realNameEnc/v1",{bizToken,wxOpenId});
		}catch(e){
			// 重复认证跳相应页面
			uni.redirectTo({
				url:'/pages/oldClient/verifyFalse2'
			});
			return Promise.reject();
		}
		await http.post("/loan/custVideo/api/submitWxEqbSignPub/v1",{wxOpenId});
		return res;
	},
	async realNameCode({state,commit}:ActionArgs){
		const wxOpenId = unicore.dom.getQueryString("wxOpenId") || unicore.util.localStorage.get("wxOpenId")
		const orderNo = unicore.dom.getQueryString("orderNo")
		const res = await http.post("/loan/wxdetectauth/api/realNameCodeEnc/v1",{wxOpenId,orderNo});
		return res;
	},
	async getRegionTreePub({state,commit}:ActionArgs,data:any){ // 获取省份城市
		const res = await http.post("/loan/region/api/getRegionTreePub/v1",data);
		return res;
	},
	async signWxBkCardPro({state,commit}:ActionArgs,data:any){ // 账户签约 获取验证码
		const res = await http.post("/loan/ggzySign/api/signWxBkCardPub/v1",data);
		return res;
	},
	async ggzySignconfirmWxBkCardPub({state,commit}:ActionArgs,data:any){ // 账户签约 绑定无误，返回继续确认
		const res = await http.post("/loan/ggzySign/api/confirmWxBkCardPub/v1",data);
		console.log(res)
		return res;
	},
	async getCaptcha({state,commit}:ActionArgs,data:any){ // 图形验证码获取
		const res = await http.post("/loan/cust/api/getCaptchaPub/v1",data);
		// console.log(res)
		return res;
	},
	async checkCaptcha({state,commit}:ActionArgs,data:any){ // 图形验证码获取
		const res:any= await http.post("/loan/cust/api/checkCaptchaPub/v1",data);
		console.log(res)
		if(res.data.repCode !='0000'){
			throw new Error("验证失败")
		}
		return res;
	},
	async ocridno({state,commit}:ActionArgs,data:any){ // ocr识别
		const res:any= await http.post("/loan/wxdetectauth/api/ocridno",data);
		return res;
	},
	async facePub({state,commit}:ActionArgs,data:any){ // 人脸识别
		const res:any= await http.post("/loan/wxdetectauth/page/initH5FacePub",data);
		return res;
	},
	/* 通用h5页面 */
	async getRegH5CodePub({state,commit}:ActionArgs,data:any){ // 获取验证码
		const res = await http.post("/loan/cust/api/getRegH5CodePub/v1",data);
		console.log(res)
		return res;
	},
	async loginNoPassThirdEnc({state,commit}:ActionArgs,data:any){ // 登录
		const res:any= await http.post("/loan/cust/api/loginNoPassThirdEnc/v1",data);
		return res;
	},
	async setPasswd({state,commit}:ActionArgs,data:any){ // 第一次登录设置密码
		const res:any= await http.post("/loan/cust/api/setPasswd/v1",data);
		return res;
	},
	async realNameEncH5({state,commit}:ActionArgs,data:any){ // 实名成功调用
		let res;
		res= await http.post("/loan/wxdetectauth/api/realNameEncH5/v1",data)
		await http.post("/loan/custVideo/api/submitWxEqbSignPub/v1",{});
		return res;
	},
	async unbindPub({state,commit}:ActionArgs,data:any){ // 解除绑定
		const res:any= await http.post("/loan/wxdetectauth/api/unbindPub",data);
		return res;
	},
}
